package com.xiaoc.bulldozer.common.service.impl;

import com.xc.framework.common.job.OneMinuteJobService;
import com.xiaoc.bulldozer.common.dao.ActionMapper;
import com.xiaoc.bulldozer.common.dao.PriviledgeMapper;
import com.xiaoc.bulldozer.common.model.Action;
import com.xiaoc.bulldozer.common.model.Priviledge;
import com.xiaoc.bulldozer.common.service.PriviledgeService;
import com.xiaoc.bulldozer.common.service.cache.PriviledgeCacheService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Priviledge Service 实现类
 * 
 * @author CodeGenerator
 * 
 * @date 2014-05-20 17:26:51
 * 
 */
@Service("priviledgeService")
public class PriviledgeServiceImpl implements PriviledgeService, PriviledgeCacheService, OneMinuteJobService {

    /**
     * 角色对应的菜单列表Map，一分钟加载一次到内存
     */
    private Map<Integer, List<Action>> roleActionListCacheMap = new HashMap<Integer, List<Action>>();
    
	@Resource
	private PriviledgeMapper priviledgeMapper;

	@Resource
    private ActionMapper actionMapper;
	
	@Override
    public void refreshCacheData() {
    
	    Map<Integer, List<Action>> roleActionListMap = new HashMap<Integer, List<Action>>();
	    List<Priviledge> priviledgeList = priviledgeMapper.getAll();
        List<Action> actionList = actionMapper.getAll();
        
        Map<Integer, Action> actionMap = new HashMap<Integer, Action>();
        for (Action action : actionList) {
            actionMap.put(action.getId(), action);
        }
        
        for (Priviledge priviledge : priviledgeList) {
            List<Action> roleActionList = roleActionListMap.get(priviledge.getRoleId());
            if (roleActionList == null) {
                roleActionList = new ArrayList<Action>();
                roleActionListMap.put(priviledge.getRoleId(), roleActionList);
            }
            
            Action action = actionMap.get(priviledge.getActionId());
            if (action != null) {
                roleActionList.add(action);
            }
        }
        
        roleActionListCacheMap = roleActionListMap;
    }
	
	@Override
    public List<Action> getActionListByRoleIdList(List<Integer> roleIdList) {
	    
	    if (MapUtils.isEmpty(roleActionListCacheMap)) {
            refreshCacheData();
        }
	    
	    Set<Action> actionSet = new TreeSet();
	    for (Integer roleId : roleIdList) {
	        List<Action> roleActionList = roleActionListCacheMap.get(roleId);
	        if (CollectionUtils.isNotEmpty(roleActionList)) {
	            actionSet.addAll(roleActionList);
	        }
        }
	    
        return new ArrayList(actionSet);
    }
	
	@Override
	public List<Priviledge> getAll() {
		return priviledgeMapper.getAll();
	}

	@Override
	public void deleteBatch(Integer roleId) {
		priviledgeMapper.deleteBatch(roleId);
	}

	@Override
	public void addBatch(Integer[] actionIdArr, Integer roleId) {
		priviledgeMapper.addBatch(actionIdArr, roleId);
	}

    @Override
    public List<Priviledge> getPriviledgeListByRoleId(Integer roleId) {
        return priviledgeMapper.getPriviledgeListByRoleId(roleId);
    }
}
